﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;
using System.Linq.Expressions;

namespace ByteSite.DAL
{
    /// <summary>
    /// 数据访问类:站点支付方式表
    /// </summary>
    public partial class site_payment
    {
        private string databaseprefix; //数据库表名前缀
        private SqlSugarClient db;
        public site_payment(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
            db = new DBUtility.DbContext().Db;
        }

        /// <summary>
        /// 获取支付平台实体
        /// </summary>
        public Entitys.dt_payment GetPaymentModel(int id)
        {
            return db.Queryable<Entitys.dt_payment, Entitys.dt_site_payment>((p, s) => new JoinQueryInfos(JoinType.Inner,p.id == s.payment_id)).Where((p, s) => s.id == id).First();
        }

        public List<Entitys.dt_site_payment> GetList(int top, Expression<Func<Entitys.dt_site_payment, bool>> whereExpression)
        {
            if (top == 0)
            {
                return db.Queryable<Entitys.dt_site_payment, Entitys.dt_payment>((s, p) => new JoinQueryInfos(JoinType.Inner, p.id == s.payment_id))
                    .Where(whereExpression)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .Select((s, p) => new Entitys.dt_site_payment
                    {
                        id = s.id,
                        site_id = s.site_id,
                        payment_id = s.payment_id,
                        title = s.title,
                        key1 = s.key1,
                        key2 = s.key2,
                        key3 = s.key3,
                        key4 = s.key4,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        img_url = p.img_url,
                        ptitle = p.title,
                        remark = p.remark,
                        poundage_type = p.poundage_type,
                        poundage_amount = p.poundage_amount,
                        redirect_url = p.redirect_url,
                        return_url = p.return_url,
                        notify_url = p.notify_url,
                        is_lock = p.is_lock
                    })
                    .ToList();
            }
            else
            {
                return db.Queryable<Entitys.dt_site_payment, Entitys.dt_payment>((s, p) => new JoinQueryInfos(JoinType.Inner, p.id == s.payment_id))
                    .Where(whereExpression)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .Select((s, p) => new Entitys.dt_site_payment
                    {
                        id = s.id,
                        site_id = s.site_id,
                        payment_id = s.payment_id,
                        title = s.title,
                        key1 = s.key1,
                        key2 = s.key2,
                        key3 = s.key3,
                        key4 = s.key4,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        img_url = p.img_url,
                        ptitle = p.title,
                        remark = p.remark,
                        poundage_type = p.poundage_type,
                        poundage_amount = p.poundage_amount,
                        redirect_url = p.redirect_url,
                        return_url = p.return_url,
                        notify_url = p.notify_url,
                        is_lock = p.is_lock
                    })
                    .Take(top)
                    .ToList();

            }
        }

        public List<Entitys.dt_site_payment> GetList(int top, string strWhere)
        {
            if (top == 0)
            {
                return db.Queryable<Entitys.dt_site_payment, Entitys.dt_payment>((s, p) => new JoinQueryInfos(JoinType.Inner, p.id == s.payment_id))
                    .Where(strWhere)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .Select((s, p) => new Entitys.dt_site_payment
                    {
                        id = s.id,
                        site_id = s.site_id,
                        payment_id = s.payment_id,
                        title = s.title,
                        key1 = s.key1,
                        key2 = s.key2,
                        key3 = s.key3,
                        key4 = s.key4,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        img_url = p.img_url,
                        ptitle = p.title,
                        remark = p.remark,
                        poundage_type = p.poundage_type,
                        poundage_amount = p.poundage_amount,
                        redirect_url = p.redirect_url,
                        return_url = p.return_url,
                        notify_url = p.notify_url,
                        is_lock = p.is_lock
                    })
                    .ToList();
            }
            else
            {
                return db.Queryable<Entitys.dt_site_payment, Entitys.dt_payment>((s, p) => new JoinQueryInfos(JoinType.Inner, p.id == s.payment_id))
                    .Where(strWhere)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .Select((s, p) => new Entitys.dt_site_payment
                    {
                        id = s.id,
                        site_id = s.site_id,
                        payment_id = s.payment_id,
                        title = s.title,
                        key1 = s.key1,
                        key2 = s.key2,
                        key3 = s.key3,
                        key4 = s.key4,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        img_url = p.img_url,
                        ptitle = p.title,
                        remark = p.remark,
                        poundage_type = p.poundage_type,
                        poundage_amount = p.poundage_amount,
                        redirect_url = p.redirect_url,
                        return_url = p.return_url,
                        notify_url = p.notify_url,
                        is_lock = p.is_lock
                    })
                    .Take(top)
                    .ToList();

            }
        }

        public List<Entitys.dt_site_payment> GetList(int pageSize, int pageIndex, Expression<Func<Entitys.dt_site_payment, bool>> whereExpression, out int recordCount)
        {
            recordCount = 0;
            //A.title as ptitle,A.img_url,A.remark,A.api_path,A.is_lock
            //.Select((s, a) => new { ptitle=a.title,img_url=a.img_url,remark=a.remark,api_path=a.api_path,is_lock=a.is_lock })
            return db.Queryable<Entitys.dt_site_payment, Entitys.dt_payment > ((s, p) => new JoinQueryInfos(JoinType.Inner, p.id == s.payment_id))
                .Where(whereExpression)
                .OrderBy(s => s.sort_id, OrderByType.Asc)
                .OrderBy(s => s.id, OrderByType.Desc)
                .Select((s, p) => new Entitys.dt_site_payment
                {
                    id = s.id,
                    site_id = s.site_id,
                    payment_id = s.payment_id,
                    title = s.title,
                    key1 = s.key1,
                    key2 = s.key2,
                    key3 = s.key3,
                    key4 = s.key4,
                    sort_id = s.sort_id,
                    add_time = s.add_time,

                    img_url = p.img_url,
                    ptitle = p.title,
                    remark = p.remark,
                    poundage_type=p.poundage_type,
                    poundage_amount=p.poundage_amount,
                    redirect_url = p.redirect_url,
                    return_url = p.return_url,
                    notify_url = p.notify_url,
                    is_lock = p.is_lock
                })
                .ToPageList(pageIndex, pageSize, ref recordCount);
        }

    }
}